//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FlatHazardRate.h"
using namespace Cephei::QL::Termstructures::Credit;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/Credit/HazardRateStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double hazardRate, Cephei::QL::Times::IDayCounter^ dayCounter) : CHazardRateStructure(CFlatHazardRate::typeid)
{
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFlatHazardRate = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Rate _hazardRate = (QuantLib::Rate)ValueHelper::Convert (hazardRate);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppFlatHazardRate = new boost::shared_ptr<QuantLib::FlatHazardRate> (new QuantLib::FlatHazardRate ( _settlementDays,  _calendar,  _hazardRate,  _dayCounter ));
        SetHazardRateStructure (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (DateTime referenceDate, Cephei::QL::IQuote^ hazardRate, Cephei::QL::Times::IDayCounter^ dayCounter) : CHazardRateStructure(CFlatHazardRate::typeid)
{
    CQuote^ _ChazardRate;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFlatHazardRate = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate);
        _ChazardRate = safe_cast<CQuote^> (hazardRate);
        _ChazardRate->Lock();
        Handle<QuantLib::Quote>& _hazardRate = static_cast<Handle<QuantLib::Quote>&> (_ChazardRate->GetHandle ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppFlatHazardRate = new boost::shared_ptr<QuantLib::FlatHazardRate> (new QuantLib::FlatHazardRate ( _referenceDate,  _hazardRate,  _dayCounter ));
        SetHazardRateStructure (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_ChazardRate != nullptr) _ChazardRate->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::IQuote^ hazardRate, Cephei::QL::Times::IDayCounter^ dayCounter) : CHazardRateStructure(CFlatHazardRate::typeid)
{
    CCalendar^ _Ccalendar;
    CQuote^ _ChazardRate;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFlatHazardRate = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        _ChazardRate = safe_cast<CQuote^> (hazardRate);
        _ChazardRate->Lock();
        Handle<QuantLib::Quote>& _hazardRate = static_cast<Handle<QuantLib::Quote>&> (_ChazardRate->GetHandle ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppFlatHazardRate = new boost::shared_ptr<QuantLib::FlatHazardRate> (new QuantLib::FlatHazardRate ( _settlementDays,  _calendar,  _hazardRate,  _dayCounter ));
        SetHazardRateStructure (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_ChazardRate != nullptr) _ChazardRate->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (DateTime referenceDate, Double hazardRate, Cephei::QL::Times::IDayCounter^ dayCounter) : CHazardRateStructure(CFlatHazardRate::typeid)
{
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFlatHazardRate = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate);
        QuantLib::Rate _hazardRate = (QuantLib::Rate)ValueHelper::Convert (hazardRate);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppFlatHazardRate = new boost::shared_ptr<QuantLib::FlatHazardRate> (new QuantLib::FlatHazardRate ( _referenceDate,  _hazardRate,  _dayCounter ));
        SetHazardRateStructure (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (boost::shared_ptr<QuantLib::FlatHazardRate>& childNative, Object^ owner) : CHazardRateStructure(CFlatHazardRate::typeid)
{
#ifdef HANDLE
	_phFlatHazardRate = NULL;
#endif
	_ppFlatHazardRate = &childNative;
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
}
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (QuantLib::FlatHazardRate& childNative, Object^ owner) : CHazardRateStructure(CFlatHazardRate::typeid)
{
#ifdef HANDLE
	_phFlatHazardRate = NULL;
#endif
	_ppFlatHazardRate = new boost::shared_ptr<QuantLib::FlatHazardRate> (&childNative);
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
    _FlatHazardRateOwner = owner;
    _HazardRateStructureOwner = owner;
}

Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (CFlatHazardRate^ copy) : CHazardRateStructure(CFlatHazardRate::typeid)
{
#ifdef HANDLE
	_phFlatHazardRate = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFlatHazardRate = new boost::shared_ptr<QuantLib::FlatHazardRate> (copy->GetShared());
        _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
    }
}
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (System::Type^ t) : CHazardRateStructure(CFlatHazardRate::typeid)
{
#ifdef HANDLE
	_phFlatHazardRate = NULL;
#endif
	if (!t->IsSubclassOf(CFlatHazardRate::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (QuantLib::Handle<QuantLib::FlatHazardRate>& childNative, Object^ owner)  : CHazardRateStructure(CFlatHazardRate::typeid)
{
	_phFlatHazardRate = &childNative;
	_ppFlatHazardRate = &static_cast<boost::shared_ptr<QuantLib::FlatHazardRate>>(childNative.currentLink());
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
    _FlatHazardRateOwner = owner;
}
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (QuantLib::Handle<QuantLib::FlatHazardRate> childNative)  : CHazardRateStructure(CFlatHazardRate::typeid)
{
	_phFlatHazardRate = &childNative;
	_ppFlatHazardRate = &static_cast<boost::shared_ptr<QuantLib::FlatHazardRate>>(childNative.currentLink());
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Credit::CFlatHazardRate::CFlatHazardRate (QuantLib::FlatHazardRate childNative)  : CHazardRateStructure(CFlatHazardRate::typeid)
{
#ifdef HANDLE
	_phFlatHazardRate = NULL;
#endif
	_ppFlatHazardRate = new boost::shared_ptr<QuantLib::FlatHazardRate> (new QuantLib::FlatHazardRate (childNative));
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFlatHazardRate));
}
#endif

Cephei::QL::Termstructures::Credit::CFlatHazardRate::~CFlatHazardRate ()
{
    if (_ppFlatHazardRate != NULL)
    {
	    delete _ppFlatHazardRate;
        _ppFlatHazardRate = NULL;
    }
}
Cephei::QL::Termstructures::Credit::CFlatHazardRate::!CFlatHazardRate ()
{
    if (_ppFlatHazardRate != NULL)
    {
	    delete _ppFlatHazardRate;
    }
}
QuantLib::FlatHazardRate& Cephei::QL::Termstructures::Credit::CFlatHazardRate::GetReference ()
{
    if (_ppFlatHazardRate == NULL) throw gcnew NativeNullException ();
	return **_ppFlatHazardRate;
}
boost::shared_ptr<QuantLib::FlatHazardRate>& Cephei::QL::Termstructures::Credit::CFlatHazardRate::GetShared ()
{
    if (_ppFlatHazardRate == NULL) throw gcnew NativeNullException ();
	return *_ppFlatHazardRate;
}
QuantLib::FlatHazardRate* Cephei::QL::Termstructures::Credit::CFlatHazardRate::GetPointer ()
{
    if (_ppFlatHazardRate == NULL) throw gcnew NativeNullException ();
	return &**_ppFlatHazardRate;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FlatHazardRate>& Cephei::QL::Termstructures::Credit::CFlatHazardRate::GetHandle ()
{
	if (_phFlatHazardRate == NULL)
	{
		_phFlatHazardRate = new Handle<QuantLib::FlatHazardRate> (*_ppFlatHazardRate);
	}
	return *_phFlatHazardRate;
}
#endif
bool Cephei::QL::Termstructures::Credit::CFlatHazardRate::HasNative () 
{
	return (_ppFlatHazardRate != NULL);
}

DateTime Cephei::QL::Termstructures::Credit::CFlatHazardRate::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppFlatHazardRate)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Credit::IFlatHazardRate^ Cephei::QL::Termstructures::Credit::CFlatHazardRate_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double hazardRate, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return gcnew CFlatHazardRate ( settlementDays,  calendar,  hazardRate,  dayCounter);
}
Cephei::QL::Termstructures::Credit::IFlatHazardRate^ Cephei::QL::Termstructures::Credit::CFlatHazardRate_Factory::Create (DateTime referenceDate, Cephei::QL::IQuote^ hazardRate, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return gcnew CFlatHazardRate ( referenceDate,  hazardRate,  dayCounter);
}
Cephei::QL::Termstructures::Credit::IFlatHazardRate^ Cephei::QL::Termstructures::Credit::CFlatHazardRate_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::IQuote^ hazardRate, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return gcnew CFlatHazardRate ( settlementDays,  calendar,  hazardRate,  dayCounter);
}
Cephei::QL::Termstructures::Credit::IFlatHazardRate^ Cephei::QL::Termstructures::Credit::CFlatHazardRate_Factory::Create (DateTime referenceDate, Double hazardRate, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return gcnew CFlatHazardRate ( referenceDate,  hazardRate,  dayCounter);
}
